/******************************************************************************
*   Filename:   SchM.c 
*
*   Description:
*
*
*   Revision History:
*
*   Date          #Change       Author     
*   MM-DD-YY      Number:       Initials   Description of change
*   -----------   -----------   --------   ------------------------------------
*   03-01-14       02            SPA        OS Task Initial Release
******************************************************************************/

/******************************************************************************
*   Include Files
******************************************************************************/ 
#include <mc9s12xep100.h>
#include "SchM.h"
#include "gpt.h"

/******************************************************************************
*   Local Macro Definitions
******************************************************************************/ 

/******************************************************************************
*   Local Type Definitions
******************************************************************************/

/******************************************************************************
*   Local Function Declarations
******************************************************************************/ 

/******************************************************************************
*   Global Variable Definitions
******************************************************************************/

/******************************************************************************
*   Static Variable Definitions
******************************************************************************/
 static u8 SchM_OSTickEnabled;
 static u8 SchM_OSTickCounter;
 static u8 SchM_SchedulerEnabled;

/******************************************************************************
*   Global and Static Function Definitions
******************************************************************************/ 

/*****************************************************************************************************                                                                        
*   Function: SchM_Init(SchM_TaskConfigType *SchM_Config)
*
*   Description: Scheduler Initialization
*
*   Caveats:
*****************************************************************************************************/
void SchM_Init(const SchM_TaskConfigType *SchM_Config)
{	

	/*Initialize timer configuration for the OS tick*/
	Gpt_Init(&Gpt_ConfigType_initial[0]);
	
  /* Initialize Ostick and Scheduler flags */
	SchM_OSTickEnabled = SCHM_OSTICK_DISABLED;
	SchM_SchedulerEnabled =  SCHEDULER_ENABLED;
	SchM_OSTickCounter = 0;	 
}


/*****************************************************************************************************                                                                        
*   Function: SchM_Init
*
*   Description: Scheduler De-initialization
*
*   Caveats:
*****************************************************************************************************/
void SchM_DeInit(void)
{
	/* Disable the Scheduler flag */
  SchM_SchedulerEnabled =  SCHEDULER_DISABLED;	
}


/*****************************************************************************************************                                                                        
*   Function: SchM_Start
*
*   Description: Starts the scheduler
*
*   Caveats:
*****************************************************************************************************/
void SchM_Start(void)
{
    /*Start the timer for the OS tick*/
    Gpt_StartTimer(CHANNEL_0, 500u);
    /*Enable notification*/
    Gpt_EnableNotification(CHANNEL_0);
}

/*****************************************************************************************************                                                                        
*   Function: SchM_OsTick
*
*   Description: Callback handled once an interrupt is generated by Channel 0.
*
*   Caveats: Non Reentrant
*****************************************************************************************************/
void SchM_OsTick(void)
{

  SchM_OSTickCounter ++;
  
  if(!SchM_OSTickEnabled)
  {
    SchM_OSTickEnabled = SCHM_OSTICK_ENABLED;
  }
  else
  {
    /* ERROR: Ostick flag was not disabled */ 
  }
  PORTB_PB2= ~PORTB_PB2;
}

/*****************************************************************************************************                                                                        
*   Function: SchM_Background
*
*   Description: BackGround Task
*
*   Caveats: Non Reentrant
*****************************************************************************************************/
void SchM_Background(void)
{
   u8 Task_Index = 0;
   if(SchM_SchedulerEnabled)
   {
     if(SchM_OSTickEnabled)
     {
      PORTB_PB3= 1;
         for(Task_Index=0;Task_Index < SchM_TaskConfigInitial[0U].SchM_TaskNumberConfig;Task_Index++)
         {
           if((SchM_OSTickCounter & SchM_TaskConfigInitial->ptr_SchM_Task[Task_Index].Mask) == 
               SchM_TaskConfigInitial->ptr_SchM_Task[Task_Index].Offset)
           {
             SchM_TaskConfigInitial->ptr_SchM_Task[Task_Index].SchM_TaskCallback();
           }
           
         }
         /*Wait for the next OS Tick to enable it*/
         SchM_OSTickEnabled = SCHM_OSTICK_DISABLED;
      PORTB_PB3= 0;
     }
     else
     {
        /*For future purposes*/
     }
   } 
   else{
     /*Scheduler is not enabled*/
   }
}
